package dna.nucleosides;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import core.molecules.IMolecule;
import core.molecules.MoleculeBase;
import core.molecules.atoms.Atom;
import core.molecules.atoms.Atom.SpecialState;
import core.molecules.organic.OrganicCompound;
import core.molecules.organic.glycosylamines.Glycosylamine;
import core.molecules.organic.phosphate.Phosphate;

import dna.nucleobase.Nucleobase;
import dna.sugars.Ribose;



/**
 * A Nucleoside specific structure designed to serve as a base for specific nuceotides and make
 * them easy to construct
 * @author Julian
 *
 */
public abstract class Nucleoside extends Glycosylamine<Ribose, Nucleobase>{

	@Override
	public boolean isEmpty() {
		return super.isEmpty() && myPhosphate.isEmpty();
	}

	@Override
	public void trimEmptyMolecules() {
		super.trimEmptyMolecules();
		if (myPhosphate.isEmpty()) myPhosphate = null;
		else myPhosphate.trimEmptyMolecules();
	}

	private Phosphate myPhosphate;
	
	public Nucleoside( Nucleobase n, Phosphate p, int id) {
		super(new Ribose(id), n, id);
		myPhosphate = p;
	}

	public Nucleoside( Nucleobase n, int id) {
		this(n, new Phosphate(id), id);
		
	}
	
	
	

	public void setPhosphate(Phosphate phosphate) {
		this.myPhosphate = phosphate;
	}

	public Phosphate getPhosphate() {
		return myPhosphate;
	}

	@Override
	public List<Atom> getAllSubAtoms() {
		List<Atom> allAtoms = super.getAllSubAtoms();
		if (this.getPhosphate() != null)
			allAtoms.addAll(getPhosphate());
		return allAtoms;
	}
	
	@Override
	public String toString() {
		String s = "";
		if (this.getSugar() != null) s += this.getSugar();
		if (this.getAmine() != null) s += this.getAmine();
		if (this.getPhosphate() != null) s += this.getPhosphate();
		return s;
	}

	/**
	 * Returns in order Phosphate, Nucleobase, Sugar
	 */
	@Override
	public List<OrganicCompound> getSubMolecules() {
		List<OrganicCompound> all = new ArrayList<OrganicCompound>();
		if (this.getPhosphate() != null)
			all.add(getPhosphate());
		all.addAll(super.getSubMolecules());
		return all;
	}

	


	
	

}
